import scala.actors._
import scala.util.Random

/**
 * BodyScan takes the given Passenger and passes it to Security if it passes.
 * Rejects Bags 20% of the time.
 * @author Samuel Nelson, Robert Vrooman, James Fitzgerald, Gabriel Marcano
 */

class BodyScan (lineNum : Int, security : Security) extends Actor{
  private val _lineNumber = lineNum;
  def lineNumber = _lineNumber;
  def act(){
    loop{
      react{
        case passenger : Passenger =>
          println("BodyScan " + lineNum + " has received Passenger "
                  + passenger.ID + ".")
          if(Random.nextDouble < .20){
            val report = new Report(passenger.ID, false)
            println("BodyScan " + lineNum +
                    " is sending Passenger " + passenger.ID + "'s failed"+
                    " Report to Security "  + security.lineNumber + ".")
            security ! report
          }
          else{
            val report = new Report(passenger.ID, true)
            println("BodyScan " + lineNum +
                    " is sending Passenger " + passenger.ID + "'s passed"+
                    " Report to Security "  + security.lineNumber + ".")
            security ! report
          }
		    
        case Shutdown =>
          println("BodyScan " + lineNum + " has received Shutdown.")
          shutdownRequested
		      
        case Startup =>
          println("BodyScan " + lineNum + " has received Startup.")
          println("BodyScan " + lineNum + " is sending Startup to Security " +
                  + security.lineNumber + ".")
          security ! Startup
      }
    }
  }
  // reactWithin(0) gives a TIMEOUT as soon as the mailbox is empty
  def shutdownRequested : Nothing = reactWithin(0){
    case Shutdown =>{
        shutdownRequested
      }
    case TIMEOUT =>{
        
        println("BodyScan " + lineNum + " is sending Shutdown to Security " +
                + security.lineNumber + ".")
        security ! Shutdown
        println("BodyScan " + lineNum + " has now shutdown.")
        exit
      }
  }
}